package kr.co.kiso.usr.iv.sr.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import kr.co.kiso.usr.iv.sr.dao.IVSRDao;
import kr.co.kiso.usr.iv.sr.vo.IVSRVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class IVSRService {
	@Autowired
	public IVSRDao dao;
	
	public int ivsr010Q_cnt(IVSRVo vo) throws Exception {
		return dao.ivsr010Q_cnt(vo);
	}
	
	public List ivsr010Q(IVSRVo vo) throws Exception {
		return dao.ivsr010Q(vo);
	}
	
	public List ivsr011Q(IVSRVo vo) throws Exception {
		return dao.ivsr011Q(vo);
	}
	
	public List ivsr012Q(IVSRVo vo) throws Exception {
		return dao.ivsr012Q(vo);
	}
	public List ivsr013Q(IVSRVo vo) throws Exception {
		return dao.ivsr013Q(vo);
	}
	
	public List ivsr014Q(IVSRVo vo) throws Exception {
		return dao.ivsr014Q(vo);
	}
	
	public Map ivsr015Q(IVSRVo vo) throws Exception {
		return dao.ivsr015Q(vo);
	}
	
	public List ivsr016Q(IVSRVo vo) throws Exception {
		return dao.ivsr016Q(vo);
	}
	
	public List ivsr017Q(IVSRVo vo) throws Exception {
		return dao.ivsr017Q(vo);
	}
	
	
	
	public int dstd020Q(IVSRVo vo) throws Exception {
		return dao.dstd020Q(vo);
	}
	
	public int dstd021Q(IVSRVo vo) throws Exception {
		return dao.dstd021Q(vo);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void ivsr020T(IVSRVo vo) throws Exception {
		vo.setIs_seq_no(dstd020Q(vo));
		dao.ivsr020T(vo);
		//파일저장하는 부분설정
		Map fileMap = vo.getFile_map();
		List fileList = (List) fileMap.get("files");
		if(fileList!=null && fileList.size()>0){
			for(int i=0; i< fileList.size();i++){
				Map file = (Map) fileList.get(i);
				vo.setFile_seq_no(dstd021Q(vo));

				vo.setM_file_nm((String) file.get("file"));
				vo.setV_file_nm((String) file.get("file_nm"));
				vo.setFile_size((String) file.get("file_sz"));
				ivsr021T(vo);	
			}
			
		}
		
	}
	public void ivsr021T(IVSRVo vo) throws Exception {
		dao.ivsr021T(vo);
	}
	
	public Map ivsr030Q(IVSRVo vo) throws Exception {
		return dao.ivsr030Q(vo);
	}
	
	public List ivsr031Q(IVSRVo vo) throws Exception {
		return dao.ivsr031Q(vo);
	}
	
	public List ivsr032Q(IVSRVo vo) throws Exception {
		return dao.ivsr032Q(vo);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public int ivsr040T(IVSRVo vo,String path) throws Exception {
		
		if(vo.getDels()!=null && vo.getDels().length>0){

			List delList = new ArrayList();
			
			for(int i=0; i < vo.getDels().length;i++){
				delList.add(vo.getDels()[i]);
			}
			vo.setDelList(delList);
			//기존파일 하드에서 삭제
			List fileList = dao.ivsr042Q(vo);
			if(null != fileList && fileList.size()>0){
				for(int i=0; i< fileList.size();i++){
					Map del = (Map) fileList.get(i);
					String delpath = (String) del.get("m_file_nm");
					File delFile = new File(path+delpath);
					if(delFile.isFile()){
						delFile.delete();
					}
				}
			}
			
			
			//기존파일 디비에서 삭제
			dao.ivsr043T(vo);
		}
		
		//파일저장하는 부분설정
		Map fileMap = vo.getFile_map();
		List fileList = (List) fileMap.get("files");
		if(fileList!=null && fileList.size()>0){
			for(int i=0; i< fileList.size();i++){
				Map file = (Map) fileList.get(i);
				vo.setFile_seq_no(dstd021Q(vo));

				vo.setM_file_nm((String) file.get("file"));
				vo.setV_file_nm((String) file.get("file_nm"));
				vo.setFile_size((String) file.get("file_sz"));
				ivsr021T(vo);	
			}
		}
		
//		정보 업데이트
		return dao.ivsr040T(vo);
	}

	public int ivsr041T(IVSRVo vo) throws Exception {
		return dao.ivsr041T(vo);
	}

	
	
	
	public int ivsr050T(IVSRVo vo) throws Exception {
		return dao.ivsr050T(vo);
	}

	public int ivsr070T(IVSRVo vo) throws Exception {
		return dao.ivsr070T(vo);
	}

	
	
}
